﻿using LianXi1013.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace LianXi1013.Repostory
{
    public class BaseRepostory<TEntity> : IBaseRepostory<TEntity>
        where TEntity : class
    {
        protected ProjectDbContext db;
        /// <summary>
        /// 根据主键id进行删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            var entity = db.Set<TEntity>().Find(id);
            db.Set<TEntity>().Remove(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据实体进行删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(TEntity entity)
        {
            db.Set<TEntity>().Remove(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据数组来进行
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int Delete(int[] idList)
        {
            foreach (var item in idList)
            {
                var entity = db.Set<TEntity>().Find(item);
                db.Set<TEntity>().Remove(entity);
            }
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据集合进行删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(List<TEntity> entity)
        {
            db.Set<TEntity>().RemoveRange(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public int Delete(Expression<Func<TEntity, bool>> condition)
        {
            var list = db.Set<TEntity>().Where(condition).ToList();
            db.Set<TEntity>().RemoveRange(list);
            return db.SaveChanges();
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        public List<TEntity> EntoityAll()
        {
            var list = db.Set<TEntity>().ToList();
            return list;
        }
        /// <summary>
        /// 查询第一个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TEntity FirstQueryable(int id)
        {
            var query = db.Set<TEntity>().Find(id);
            return query;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public TEntity Insert(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
            db.SaveChanges();
            return entity;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int PutEntity(TEntity entity)
        {
            db.Set<TEntity>().Update(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> condition)
        {
            var list = db.Set<TEntity>().Where(condition).ToList();
            return list;
        }
        /// <summary>
        /// Queryable查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<TEntity> Queryable()
        {
            var query = db.Set<TEntity>().AsQueryable();
            return query;
        }
    }
}
